/**
 * 执行自定义的公式
 * 支持toHead 表体数据对表头影响
 * toBody 表体页签之间字段影响
 */
function myDefEditInvoke(formular, ds, event){
	var formulars = decodeURIComponent(formular).split(";");
	for(var i=0,len=formulars.length; i<len; i++){
		var f = formulars[i];
		var reg1 = new RegExp("\\)", "g");
		f = f.replace(reg1, "#");
		var reg2 = new RegExp("\\(", "g"); 
		f = f.replace(reg2, '#');
		var reg3 = new RegExp("\\,", "g");
		f = f.replace(reg3, "#");
		var fArr = f.split('#'); 
		if(fArr[0] == 'toHead'){
			var targetKey = fArr[1]; 
			var func = fArr[2];
			var fromRowIndex = fArr[3];
			var fromKey = fArr[4];
			var fromTableCode = fArr[5];
			var headDs = getHeadDs();
			var targetIndex = headDs.nameToIndex(targetKey);
			var dt = headDs.metadata[targetIndex].dataType;
			var result = $getForValue(func, fromRowIndex, fromKey, fromTableCode, ds);
			if(dt == DataType.INTEGER || dt == DataType.INT)
				result = parseInt(result);
			else if(dt == DataType.DOUBLE || dt == DataType.dOUBLE || dt == DataType.Decimal || dt == DataType.UFDOUBLE)
				result = parseFloat(result);
			headDs.setValueAt(0, targetIndex, result,true);
		}
		else if(fArr[0] == 'toBody'){
			var targetRowIndex = fArr[1];
			var targetKey = fArr[2];
			var targetTableCode = fArr[3];
			var func = fArr[4];
			var fromRowIndex = fArr[5];
			var fromKey = fArr[6];
			var fromTableCode = fArr[7]; 

			var result = $getForValue(func, fromRowIndex, fromKey, fromTableCode, ds);
			var targetIndex = ds.nameToIndex(targetKey);

			var dt = ds.metadata[targetIndex].dataType;
			if(dt == DataType.INTEGER || dt == DataType.INT)
				result = parseInt(result);
			else if(dt == DataType.DOUBLE || dt == DataType.dOUBLE || dt == DataType.Decimal || dt == DataType.UFDOUBLE)
				result = parseFloat(result);
			
			for(var i=0; i<ds.getRowCount();i++){
				var code = ds.getRow(i).getCellValue(ds.nameToIndex('tablecode'))
				if(code != null && code == targetTableCode){
					ds.setValueAt(i, targetIndex, result,true);
					break;//只设置第一行
				}
			}
		}
		else if(fArr[0] == 'getReimvalue'){
		}
	}
}
function $getForValue(func, fromRowIndex, fromKey, fromTableCode, ds){
	var resultArray = new Array();
	var datatype = ds.metadata[ds.nameToIndex(fromKey)].dataType;
	if(fromRowIndex == '%all%'){
		for(var i=0,count=ds.getRowCount(); i<count; i++){
			var tablecode = ds.getRow(i).getCellValue(ds.nameToIndex('tablecode'));
			if(tablecode != null && tablecode == fromTableCode){
				var result = ds.getRow(i).getCellValue(ds.nameToIndex(fromKey));
				if(result == null || result == '')
					continue;
				if(datatype == DataType.INTEGER || datatype == DataType.INT)
					result = parseInt(result);
				else if(datatype == DataType.DOUBLE || datatype == DataType.dOUBLE 
						|| datatype == DataType.Decimal || datatype == DataType.UFDOUBLE)
					result = parseFloat(result);
				resultArray.push(result);
			}
		}
	}
	if(func == 'sum'){
		var returnResult = 0 ;
		for(var k=0,len=resultArray.length; k<len; k++){
			returnResult += resultArray[k];
		}
		return returnResult;
	}else if(func == 'max'){
		return $getMaxValue(resultArray, datatype); 
	
	}else if(func == 'min'){
		return $getMinValue(resultArray, datatype);
	}
}

function $getMaxValue(arr,datatype){

	if(datatype == DataType.UFDATE){
		arr = arr.sort(dateSortFunc);
	}else if(datatype == DataType.INTEGER || datatype == DataType.INT){
		arr = arr.sort(intSortFunc);
	}else if(datatype == DataType.DOUBLE || datatype == DataType.dOUBLE || datatype == DataType.Decimal){
		arr = arr.sort(floatSortFunc);
	}
	return arr[arr.length-1];
}
function $getMinValue(arr,datatype){
	if(datatype == DataType.UFDATE){
		arr = arr.sort(dateSortFunc);
	}else if(datatype == DataType.INTEGER || datatype == DataType.INT){
		arr = arr.sort(intSortFunc);
	}else if(datatype == DataType.DOUBLE || datatype == DataType.dOUBLE || datatype == DataType.Decimal){
		arr = arr.sort(floatSortFunc);
	}
	return arr[0];
}
function dateSortFunc(a ,b)
{
	var aa  = Date.parse(a.replace(/\-/g,"/")); 
  var bb  = Date.parse(b.replace(/\-/g,"/")); 
	return aa - bb;	
};
function intSortFunc(a ,b)
{
	return a-b;	
};
function floatSortFunc(a ,b)
{
	return a-b;	
};